//
//  XMUserHomePageView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/7/1.
//

#import "XMUserHomePageView.h"
#import "XMHomePagePorductCell.h"

@interface XMUserHomePageView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end

@interface XMUserHomePageView ()

@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *idNoLabel;
@property (nonatomic, strong) UILabel *introLabel;  //简介
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UILabel *meetNumLabel;
@property (nonatomic, strong) UIButton *attentionBtn;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *valueLabelArr;
@property (nonatomic, strong) NSArray *productModelArr;

@end

@implementation XMUserHomePageView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    UIImageView *topBgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(476.0))];
    topBgImgView.image = ImageNamed(@"detail_homePage_top_img");
    topBgImgView.userInteractionEnabled = YES;
    [self addSubview:topBgImgView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(0, kNavagationBarH - 44, 44, 44) image:@"back_icon_white" selectImg:@"back_icon_white"];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [topBgImgView addSubview:backBtn];
    {
        [topBgImgView addSubview:self.avatarImgView];
        [topBgImgView addSubview:self.nameLabel];
        [topBgImgView addSubview:self.idNoLabel];
        [topBgImgView addSubview:self.introLabel];
        [topBgImgView addSubview:self.addressLabel];
        NSArray *titleArr = @[@"获赞", @"互关", @"粉丝"];
        for (int i = 0; i < 3; i ++) {
            UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(52.0)*i, _addressLabel.maxY + KScaleWidth(15.0), KScaleWidth(52.0), KScaleWidth(25.0)) font:KBoldFontSize(18.0) textColor:kColorHEX(0xffffff) textAlignment:NSTextAlignmentCenter];
            [self.valueLabelArr addObject:valueLabel];
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(valueLabel.x, valueLabel.maxY, valueLabel.width, KScaleWidth(20.0)) font:KFontSize(14.0) textColor:kColorHEXA(0xffffff, 0.84) textAlignment:NSTextAlignmentCenter];
            titleLabel.text = titleArr[i];
            [topBgImgView addSubview:valueLabel];
            [topBgImgView addSubview:titleLabel];
        }
        UIButton *meetListBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(KScaleWidth(3.0), _addressLabel.maxY + KScaleWidth(80.0), KScaleWidth(94.0), KScaleWidth(80.0)) image:@"detail_homePage_meetbox_icon" selectImg:@"detail_homePage_meetbox_icon"];
        [meetListBtn addTarget:self action:@selector(chatMeetListAction:) forControlEvents:UIControlEventTouchUpInside];
        [topBgImgView addSubview:meetListBtn];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(meetListBtn.maxX + KScaleWidth(6.0), meetListBtn.y + KScaleWidth(15.0), self.width - meetListBtn.maxX - KScaleWidth(6.0), KScaleWidth(22.0)) font:KBoldFontSize(16.0) textColor:kColorHEXA(0xffffff, 0.84)];
        titleLabel.text = @"TA的橱窗";
        [topBgImgView addSubview:titleLabel];
        _meetNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.x, titleLabel.maxY + KScaleWidth(6.0), titleLabel.width, KScaleWidth(21.0)) font:KFontSize(15.0) textColor:kColorHEXA(0xffffff, 0.58)];
        [topBgImgView addSubview:_meetNumLabel];
        [topBgImgView addSubview:self.attentionBtn];
        _attentionBtn.y = meetListBtn.y + KScaleWidth(20.0);
        UIButton *chatBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(self.width - KScaleWidth(2.0) - KScaleWidth(57.0), _attentionBtn.y - (KScaleWidth(30.0) - KScaleWidth(28.0))/2, KScaleWidth(57.0), KScaleWidth(30.0)) image:@"detail_homePage_chat_icon" selectImg:@"detail_homePage_chat_icon"];
        [chatBtn addTarget:self action:@selector(chatAction:) forControlEvents:UIControlEventTouchUpInside];
        [topBgImgView addSubview:chatBtn];
        
        UIView *titleBgView = [[UIView alloc] initWithFrame:CGRectMake(0, topBgImgView.height - KScaleWidth(50.0), self.width, KScaleWidth(50.0))];
        titleBgView.backgroundColor = kColorHEX(0xffffff);
        [titleBgView setPartCornerWithRectCorner:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:KScaleWidth(16.0)];
        [topBgImgView addSubview:titleBgView];
        
        UILabel *topTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(12.0), 0, titleBgView.width - KScaleWidth(12.0), titleBgView.height) font:KBoldFontSize(16.0) textColor:kColorHEX(0x333333)];
        topTitleLabel.text = @"作品";
        [titleBgView addSubview:topTitleLabel];
    }
    [self addSubview:self.collectionView];
}

- (void)setContentWithUserInfoModel:(XMUserInfoModel *)userInfoModel
                             userId:(nonnull NSString *)userId {
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:userInfoModel.avatar] placeholderImage:ImageNamed(@"mine_avatar_icon")];
    _nameLabel.text = userInfoModel.nickname;
    _idNoLabel.text = [NSString stringWithFormat:@"ID：%@", userInfoModel.idNo];
    _addressLabel.text = [NSString stringWithFormat:@"%@%@", userInfoModel.province, userInfoModel.city];
    _introLabel.text = userInfoModel.dsc;
    _meetNumLabel.text = [NSString stringWithFormat:@"%@个产品", userInfoModel.serviceNum];
    for (int i = 0; i < self.valueLabelArr.count; i ++) {
        UILabel *valueLabel = self.valueLabelArr[i];
        valueLabel.text = @[userInfoModel.likeCount, userInfoModel.huguanCount, userInfoModel.fensi][i];
    }
    XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
    if ([userModel.userId isEqualToString:userId]) {
        _attentionBtn.hidden = YES;
    }
    else {
        if ([userInfoModel.follow boolValue] ) {
            [_attentionBtn setTitle:@"已关注" forState:UIControlStateNormal];
        }
        else {
            [_attentionBtn setTitle:@"关注" forState:UIControlStateNormal];
        }
    }
}

- (void)setContentWithProductModelArr:(NSArray *)productModelArr {
    _productModelArr = productModelArr;
    [_collectionView reloadData];
}

#pragma mark - Init
//查看橱窗
- (void)chatMeetListAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkMeetListAction)]) {
        [self.delegate checkMeetListAction];
    }
}

//关注
- (void)attentinoAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(attentionAction)]) {
        [self.delegate attentionAction];
    }
}

//聊天
- (void)chatAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatAction)]) {
        [self.delegate chatAction];
    }
}

#pragma mark - Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _productModelArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMHomePagePorductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XMHomePagePorductCellName" forIndexPath:indexPath];
    [cell setContentWithProductModel:_productModelArr[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

#pragma mark - Init
- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(14.0), kNavagationBarH + KScaleWidth(10.0), KScaleWidth(78.0), KScaleWidth(78.0))];
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImgView.maxX + KScaleWidth(15.0), _avatarImgView.y + KScaleWidth(14.0), self.width - _avatarImgView.maxX - KScaleWidth(15.0), KScaleWidth(25.0)) font:KBoldFontSize(18.0) textColor:kColorHEX(0xffffff)];
    }
    return _nameLabel;
}

- (UILabel *)idNoLabel {
    if (!_idNoLabel) {
        _idNoLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.x, _nameLabel.maxY + KScaleWidth(5.0), _nameLabel.width, KScaleWidth(17.0)) font:KFontSize(12.0) textColor:kColorHEXA(0xffffff, 0.6)];
    }
    return _idNoLabel;
}

- (UILabel *)introLabel {
    if (!_introLabel) {
        _introLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(13.0), self.avatarImgView.maxY + KScaleWidth(20.0), self.width - KScaleWidth(13.0), KScaleWidth(20.0)) font:KFontSize(14.0) textColor:kColorHEXA(0xffffff, 0.84)];
    }
    return _introLabel;
}

- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(13.0), self.introLabel.maxY + KScaleWidth(16.0), self.width - KScaleWidth(13.0), KScaleWidth(26.0)) font:KFontSize(14.0) textColor:kColorHEXA(0xffffff, 0.53)];
    }
    return _addressLabel;
}

- (UIButton *)attentionBtn {
    if (!_attentionBtn) {
        _attentionBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(self.width - KScaleWidth(60.0) - KScaleWidth(64.0), 0, KScaleWidth(64.0), KScaleWidth(28.0)) title:@"关注" titleColor:kColorHEX(0x333333)];
        [_attentionBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
        [_attentionBtn setCornerRadius:_attentionBtn.height/2];
        [_attentionBtn addTarget:self action:@selector(attentinoAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _attentionBtn;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(KScaleWidth(8.0), KScaleWidth(8.0), KScaleWidth(8.0), KScaleWidth(8.0));
        layout.minimumInteritemSpacing = KScaleWidth(8.0);
        layout.minimumLineSpacing = KScaleWidth(8.0);
        CGFloat width = floor((self.width - KScaleWidth(8.0)*4)/3);
        layout.itemSize = CGSizeMake(width, KScaleWidth(154.0));
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, KScaleWidth(476.0), self.width, self.height - KScaleWidth(476.0) - kBottomLayout) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = kColorHEX(0xF5F5F5);
        [_collectionView registerClass:[XMHomePagePorductCell class] forCellWithReuseIdentifier:@"XMHomePagePorductCellName"];
    }
    return _collectionView;
}

- (NSMutableArray *)valueLabelArr {
    if (!_valueLabelArr) {
        _valueLabelArr = [NSMutableArray array];
    }
    return _valueLabelArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
